package com.sky.mapper;


import com.github.pagehelper.Page;
import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.entity.Top10;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.OrderStatisticsVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    /**
     * 用户下单，插入订单信息
     * @param orders
     */

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("INSERT INTO orders" +
            "    (number, status, user_id, address_book_id, order_time, checkout_time," +
            "     pay_method, pay_status, amount, remark, user_name, phone, address," +
            "     consignee, cancel_reason, rejection_reason, cancel_time," +
            "     estimated_delivery_time, delivery_status, delivery_time, pack_amount," +
            "     tableware_number, tableware_status)" +
            "    VALUES" +
            "    (#{number}, #{status}, #{userId}, #{addressBookId}, #{orderTime}," +
            "     #{checkoutTime}, #{payMethod}, #{payStatus}, #{amount}, #{remark}," +
            "     #{userName}, #{phone}, #{address}, #{consignee}, #{cancelReason}," +
            "     #{rejectionReason}, #{cancelTime}, #{estimatedDeliveryTime}," +
            "     #{deliveryStatus}, #{deliveryTime}, #{packAmount}, #{tablewareNumber}," +
            "     #{tablewareStatus})")
    void insertOrder(Orders orders);

    /**
     * 向订单详情表中插入商品数据
     * @param orderDetail
     */
    @Insert("insert into order_detail(name, order_id, dish_id, setmeal_id, dish_flavor, number, amount, image)" +
            "    VALUES" +
            "    (#{name}, #{orderId}, #{dishId}, #{setmealId}, #{dishFlavor}, #{number}, #{amount}, #{image})")
    void insertOrderDetail(OrderDetail orderDetail);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 分页查询历史订单
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> selectOrdersPage(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据订单id查询订单明细
     * @param orderId
     * @return
     */
    @Select("select * from order_detail where order_id = #{orderId}")
    List<OrderDetail> getOrderDetailsByOrderId(Long orderId);

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);


    /**
     * 查询各订单数量
     * @return
     */
    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer status);

    @Select("select * from orders where status = 1 and order_time < now() - interval 15 minute")
    List<Orders> checkOutTimeOrders();

    @Select("select * from orders where status = 4 and order_time < now() - interval 1 hour")
    List<Orders> checkDeliveryOrder();

    /**
     * 获取指定日期的营业额
     * @param map
     * @return
     */
    Double getTurnover(Map map);

    /**
     *
     * @param map
     * @return
     */
    Integer getOrdersNumber(Map map);

    List<Top10> getSalesTop10Report(LocalDateTime begin, LocalDateTime end);

    BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end);

    Integer getTodayOrdersCount(LocalDateTime begin, LocalDateTime end);

    /**
     * 查询订单管理数据
     * @param status
     * @return
     */
    Integer getOrders(Integer status);
}
